package com.chunlin.package3;

public class CodeBlock {
    //注意事项
    //静态代码块不管生成多少个对象，其只会执行一次
    //静态成员变量是类的属性，因此是在JVM加载类时开辟空间并初始化的
    //如果一个类中包含多个静态代码块，在编译代码时，编译器会按照定义的先后次序依次执行(合并)
    //实例代码块只有在创建对象时才会执行
    public class Student1 {
        private String name;
        private String gender;
        private int age;
        private static String classRoom;

        //实例代码块,可以类比C++的初始化列表
        {
            this.name = "chunlin";
            this.age = 12;
            this.gender = "man";
            System.out.println("I am instance init()!");
        }

        // 静态代码块
        static {
            classRoom = "306";
            System.out.println("I am static init()!");
        }

        public Student1() {
            System.out.println("I am Student init()!");
        }

        public void show() {
            System.out.println("name: " + name + " age: " + age + " gender: " + gender);
        }
    }


    public static void main(String[] args) {
        //普通代码块：定义在方法中的代码块
        { //直接使用{}定义，普通方法块
            int x = 10;
            System.out.println("x1 = " + x);
        }
        int x = 100;
        System.out.println("x2 = " + x);

        CodeBlock codeBlock = new CodeBlock();

        Student1 student1 = codeBlock.new Student1();
        student1.show();
    }
}
